﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace StateMachineXNA
{
    class StateMainMenu : IGameState
    {
        private SpriteFont font;
        private Vector2 position, position_info;
        private float scale;
        private Timer timer;

        public StateMainMenu(int screen_w, int screen_h)
        {
            position = new Vector2(screen_w / 2, screen_h / 3);
            scale = 0;
            timer = new Timer(Math.PI);

            position_info = new Vector2(screen_w / 2, screen_h * (3.0f / 4.0f));
        }

        public void Init(Game1 game)
        {
            if (font == null) font = game.Content.Load<SpriteFont>("Font");
        }

        public void Pause()
        { }

        public void Resume()
        {
            timer.Restart();
            scale = (float)((1 + Math.Cos(timer.ElapsedTime * 4)) / 4 + 0.5);
        }

        public void Update(StateMachine<IGameState> sm, GameTime gt)
        {
            if (timer.Increment(gt.ElapsedGameTime.TotalSeconds))
                  timer.Restart(true);

            scale = (1 + (float)Math.Cos(timer.ElapsedTime * 6)) / 4;

            if (Input.KeyPressed(Keys.Escape))
                sm.Pop();
            else if (Input.KeyPressed(Keys.Enter))
                sm.Push(new StateGame(position, timer.ElapsedTime));
        }

        public void Draw(SpriteBatch sb)
        {
            String text = "Main Menu";
            Vector2 origin = font.MeasureString(text);
            sb.DrawString(font, text, position, Color.Green, 0.0f, new Vector2(origin.X / 2, origin.Y), new Vector2((scale + 0.5f), (1 - scale) * 2), SpriteEffects.None, 0.0f);

            string info = "Press enter to play!";
            sb.DrawString(font, info, position_info, Color.Green, 0, font.MeasureString(info) / 2, 0.3f, SpriteEffects.None, 0.0f);
        }
    }
}
